package com.vincent.sms;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Telephony;
import android.telephony.SmsMessage;

import androidx.core.app.NotificationCompat;

/**
 * projectName:          Sms
 * packageName:          com.vincent.sms
 * className:            SmsReceiver
 * author:               Luoxiang
 * time:                 2025/6/14	18:05
 * desc:                 TODO
 * <p>
 * svnVersion:
 * upDateAuthor:         a
 * upDate:               2025/6/14
 * upDateDesc:           TODO
 */

public class SmsReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            Object[] pdus = (Object[]) bundle.get("pdus");
            if (pdus != null) {
                SmsMessage[] messages = new SmsMessage[pdus.length];
                for (int i = 0; i < pdus.length; i++) {
                    messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                }

                String sender = messages[0].getDisplayOriginatingAddress();
                StringBuilder body = new StringBuilder();
                for (SmsMessage message : messages) {
                    body.append(message.getMessageBody());
                }

                // 处理接收到的短信
                processReceivedSms(context, sender, body.toString());
            }
        }
    }

    private void processReceivedSms(Context context, String sender, String body) {
        // 保存短信到内容提供者
        ContentValues values = new ContentValues();
        values.put("address", sender);
        values.put("body", body);
        values.put("date", System.currentTimeMillis());
        values.put("read", 0); // 未读
        values.put("type", 1); // 1表示收件箱

        context.getContentResolver().insert(Uri.parse("content://sms/inbox"), values);

        // 通知用户有新短信
        showNotification(context, sender, body);
    }

    private void showNotification(Context context, String sender, String body) {
        NotificationManager manager = (NotificationManager)
                context.getSystemService(Context.NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(
                    "sms_channel", "SMS Messages", NotificationManager.IMPORTANCE_HIGH);
            manager.createNotificationChannel(channel);
        }

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "sms_channel")
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setContentTitle(sender)
                .setContentText(body)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setAutoCancel(true);

        manager.notify(1, builder.build());
    }
}

